﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DTO;

namespace IndexMarkWcfService.Caches
{
    public class IndexCache: CacheInvokerBase
    {
        private static List<IndexRow> _indexes = new List<IndexRow>();

        private static int TermID;
        private static IEnumerable<int> Cats;
        private static IEnumerable<int> Chars;
        private static IEnumerable<int> Respondents;
        private static DTO.IndexListType Type;
        
        public IndexCache()
        {

        }

        public override object[] AllocateInputs()
        {
            return BaseInvoker.AllocateInputs();
        }

        public override object Invoke(object instance, object[] inputs, out object[] outputs)
        {
            IEnumerable<IndexRow> result = new List<IndexRow>();
            int TermIDParam = (int)inputs[0];
            IEnumerable<int> CatsParam = (IEnumerable<int>)inputs[1];
            IEnumerable<int> CharsParam = (IEnumerable<int>)inputs[2];
            IEnumerable<int> RespondentsParam = (IEnumerable<int>)inputs[3];
            IndexListType TypeParam = (IndexListType)inputs[4];

            if ((TermIDParam == TermID) && (CatsParam.MyEquals(Cats)) && (CharsParam.MyEquals(Chars)) && (RespondentsParam.MyEquals(Respondents)) && (TypeParam == Type) && Indexes.Count > 0) //если ВСЕ пераметры совпадают и список индексов не пустой
            {
                result = Indexes;
            }
            else
            {
                result = BaseInvoker.Invoke(instance, inputs, out outputs) as IEnumerable<IndexRow>;
                TermID = TermIDParam;
                Cats = CatsParam;
                Chars = CharsParam;
                Respondents = RespondentsParam;
                Type = TypeParam;
                _indexes = result.ToList();
            }

            outputs = new object[] { };
            return result;
        }

        public override IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
        {
            return BaseInvoker.InvokeBegin(instance, inputs, callback, state);
        }

        public override object InvokeEnd(object instance, out object[] outputs, IAsyncResult result)
        {
            return BaseInvoker.InvokeEnd(instance, out outputs, result);
        }

        public override bool IsSynchronous
        {
            get { return true; }
        }

        public static List<IndexRow> Indexes
        {
            get { return _indexes; }
        }

        public static void Clear()
        {
            Indexes.Clear();
        }
        
    }

    public static class IEnumerableExtentions
    {
        public static bool MyEquals(this IEnumerable<int> obj, IEnumerable<int> left)
        {
            if (left == null) return false;

            if (obj.Count() != left.Count()) return false;
            
            int i = 0;
            foreach (int item in obj)
            {
                if (item != left.ElementAt(i)) return false;
                i++;
            }
            return true;
        }
    }

}